package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.PromotionQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.PromotionDao;
import cn.org.eshow.model.Promotion;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("promotionDao")
public class PromotionDaoHibernate extends GenericDaoHibernate<Promotion, Integer> implements PromotionDao {

    public PromotionDaoHibernate() {
        super(Promotion.class);
    }

    @Override
    public List<Promotion> list(PromotionQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<Promotion> search(PromotionQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(PromotionQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(Promotion.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, PromotionQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startStartTime != null) {
                rule.add(Restrictions.ge("startTime", query.startStartTime));
            }
            if (query.endStartTime != null) {
                rule.add(Restrictions.le("startTime", query.endStartTime));
            }
            if (query.startEndTime != null) {
                rule.add(Restrictions.ge("endTime", query.startEndTime));
            }
            if (query.endEndTime != null) {
                rule.add(Restrictions.le("endTime", query.endEndTime));
            }
            if (query.name != null) {
                rule.add(Restrictions.like("name", query.name, MatchMode.ANYWHERE));
            }
            if (query.minDiscount != null) {
                rule.add(Restrictions.ge("discount", query.minDiscount));
            }
            if (query.maxDiscount != null) {
                rule.add(Restrictions.le("discount", query.maxDiscount));
            }
            if (query.ifLimit != null) {
                rule.add(Restrictions.eq("ifLimit", query.ifLimit));
            }
            if (query.limitNumber != null) {
                rule.add(Restrictions.eq("limitNumber", query.limitNumber));
            }
            if (query.state != null) {
                rule.add(Restrictions.eq("state", query.state));
            }
            if (query.refund != null) {
                rule.add(Restrictions.eq("refund", query.refund));
            }
            if (query.enabled != null) {
                rule.add(Restrictions.eq("enabled", query.enabled));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
